package hpasDAO;

import hpasDataEntities.*;
import javax.swing.*;
import java.sql.*;
import java.util.Vector;

/** 硬件数据访问类 **/
public class HardwareItemDAO
{
    private Connection conn;
    private HardwareDAO hardwareDAO;

    /** 创建数据库访问接口 **/
    public HardwareItemDAO()
    {
        try
        {
            connect();
        }
        catch (SQLException e)
        {
            JOptionPane.showMessageDialog(null,"数据库建立连接失败",
                    "错误",JOptionPane.ERROR_MESSAGE);
        }
        catch (ClassNotFoundException e)
        {
            JOptionPane.showMessageDialog(null,"找不到MySQL驱动程序",
                    "错误",JOptionPane.ERROR_MESSAGE);
        }
    }
    /** 创建连接 **/
    private void connect() throws SQLException, ClassNotFoundException
    {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hpas","root", "niuniu53");
    }
    public void setHardwareDAO(HardwareDAO hardwareDAO)
    {
        this.hardwareDAO = hardwareDAO;
    }
    /** 使用id精准定位硬件 **/
    public HardwareItem locateHardwareItemById(int id) throws SQLException
    {
        Statement statement = conn.createStatement();
        ResultSet resultSet = statement.executeQuery(
                "select itemId, hardwareId, retailerId, price " +
                        "from hardwareitem " +
                        "where id=" + id);
        resultSet.next();
        HardwareItem entry = new HardwareItem();
        entry.setItemId(resultSet.getInt(1));
        entry.setHardwareId(resultSet.getInt(2));
        entry.setRetailerId(resultSet.getInt(3));
        entry.setPrice(resultSet.getBigDecimal(4));
        return entry;
    }
    /** 根据硬件商品ID找到对应硬件 **/
    public Hardware getHardwareInfo(int id) throws SQLException
    {
        Hardware target = hardwareDAO.locateHardwareById(id);
        return target;
    }
    /** 添加新硬件商品 **/
    public void addHardwareItem(HardwareItem hardwareItem) throws SQLException
    {
        Statement statement = conn.createStatement();
        statement.executeUpdate("insert into hardwareitem " +
                "(itemId, hardwareId, retailerId, price) " +
                "values (" +
                hardwareItem.getItemId() + "," +
                hardwareItem.getHardwareId() + "," +
                hardwareItem.getRetailerId() + "," +
                hardwareItem.getPrice() +
                ")");
    }
    /** 更新硬件信息 **/
    public void updateHardwareItem(HardwareItem newItem) throws SQLException
    {
        HardwareItem hardwareToUpdate = locateHardwareItemById(newItem.getItemId());
        Statement statement = conn.createStatement();
        statement.executeUpdate("update hardwareitem set " +
                "itemId=\'" + newItem.getItemId() + "\'," +
                "hardwareId=\'" + newItem.getHardwareId() + "\'," +
                "retailerId=" + newItem.getRetailerId() + "," +
                "price=" + newItem.getPrice() +
                " where itemId=" + hardwareToUpdate.getItemId());
    }
    /** 删除指定硬件 **/
    public void removeHardwareItem(HardwareItem hardwareItem) throws SQLException
    {
        Statement statement = conn.createStatement();
        statement.executeUpdate("delete from hardwareitem where itemId=" + hardwareItem.getItemId());
    }
}